package com.leaf.lowcode.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.leaf.lowcode.datasend.XGFunctionApi;
import com.leaf.lowcode.entity.ResBean;
import com.leaf.lowcode.utils.RegularInstance;
import com.tencent.welink.iot.SubDeviceDTO;
import com.tencent.welink.iot.WelinkHttp;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SendDateRoutePlan {
   @Scheduled(fixedRate = 60 * 1000)
   public void excute()
    {
        ResBean resBean = new ResBean();
        XGFunctionApi xgFunctionApi = new XGFunctionApi();
        resBean.setPageNo(1);
        resBean.setPageSize(100);
        String routePlan = xgFunctionApi.getRoutePlan(resBean);
        JSONObject jsonObject = JSONObject.parseObject(routePlan);
        JSONArray RoutePlaneList = (JSONArray) ((JSONObject) jsonObject.get("data")).get("rows");
        List<SubDeviceDTO> subDeviceDTOList = RegularInstance.subDeviceDTOList;
        if (subDeviceDTOList != null) {
            for (SubDeviceDTO dto : subDeviceDTOList) {
                String pid = dto.getPid();
                String sn = dto.getSn();
                String din = dto.getDin();
                if (sn.equals("RoutePlan")) {
                    System.out.println("===========巡更计划================");
                    JSONObject standardData = new JSONObject();
                    standardData.put("reportTs", System.currentTimeMillis());
                    JSONObject profile = new JSONObject();
                    profile.put("modelId", 200669);
                    profile.put("poiCode", "w0705000");
                    standardData.put("profile", profile);
                    WelinkHttp wh = new WelinkHttp(RegularInstance.registerData1, RegularInstance.loginData);
                    JSONObject properties = new JSONObject();
                    properties.put("RoutePlaneList",RoutePlaneList);
                    standardData.put("properties", properties);
                    standardData.put("events", new JSONObject());
                    //standardData.put("properties", "{}");
                    try {
                        System.out.println("上传数据");
                        String devRunEvent = JSONObject.toJSONString(standardData, SerializerFeature.WriteMapNullValue);
                          System.out.println("发送设备运行时事件数据==" + devRunEvent);
                         System.out.println("--------------------------------开始传输设备运行时事件数据");
                        wh.sendMessage(RegularInstance.loginData.load(), 80000, Long.parseLong(din), devRunEvent, true);

                    } catch (Exception e) {
                         System.out.println("设备运行时事件数据传输出现异常");
                    }
                }
            }
        }

    }
}
